﻿using Newtonsoft.Json;
using PublicFunctions;
using PublicFunctions.Service;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;

namespace Plugins
{
    public class CheckListPlugins : ServiceFactoryBase
    {
        #region ======Configuration======
        public static string InterfaceUrl = ConfigurationManager.AppSettings["InterfaceUrl"].ToString();
        public static string PrivateKey = ConfigurationManager.AppSettings["PrivateKey"].ToString();
        public static string HisAppID = ConfigurationManager.AppSettings["HisAppID"].ToString();
        public static string OAAppID = ConfigurationManager.AppSettings["OAAppID"].ToString();
        #endregion

        #region ======构造函数======
        public CheckListPlugins(ServiceDataModel ServiceData) : base(ServiceData)
        {
        }
        #endregion

        #region ======调用业务系统检查单详情接口======
        /// <summary>
        /// 调用业务系统检查单详情接口
        /// </summary>
        /// <returns></returns>
        public AjaxResultModel GetCheckListDetail()
        {
            LogUtils.LogUtil.Info($"======调用业务系统检查单详情接口======");
            string orderNo = PublicFunctions.PublicFunctions.GetDataFromDictionary("orderNo", ServiceData.RequestFormDictionary);
            LogUtils.LogUtil.Info($"接收到的参数：orderNo:{orderNo}");
            AjaxResultModel AjaxResultObject = new AjaxResultModel();
            string strJson = "{\"orderNo\":\"" + orderNo + "\"}";
            string signature = RSAFromPkcs.sign(strJson, PrivateKey); //签名
            Header header = new Header();
            header.protocol = "POST"; //请求方式
            header.source = OAAppID; //调用系统的appid
            header.target = HisAppID; //被调用系统的appid
            header.type = "signature";//签名方式调用
            header.path = "/openapi/examinePrescription/detail";//调用的方法
            string headerJson = JsonConvert.SerializeObject(header);
            string headerBase64 = Base64Encrypt.EncodeBase64(Encoding.UTF8, headerJson); //Base64编码
            NameValueCollection headerCollection = new NameValueCollection();
            headerCollection.Add("appid", HisAppID);
            headerCollection.Add("signature", signature);
            headerCollection.Add("header", headerBase64);
            var result = HttpUtil.Post(InterfaceUrl, strJson, "application/json;charset=UTF-8", headerCollection);
            LogUtils.LogUtil.Info($"调用业务系统检查单详情接口结果集：{JsonConvert.SerializeObject(result)}");
            if (result.StatusCode == 200)
            {
                AjaxResultObject.IsSuccess = true;
                AjaxResultObject.Result = result.Result;
            }
            else
                AjaxResultObject.Result = null;
            return AjaxResultObject;
        }
        #endregion

        #region ======调用业务系统检查单检查项列表接口======
        /// <summary>
        /// 调用业务系统检查单检查项列表接口
        /// </summary>
        /// <returns></returns>
        public AjaxResultModel GetCheckListItem()
        {
            LogUtils.LogUtil.Info($"======调用业务系统检查单检查项列表接口======");
            string code = PublicFunctions.PublicFunctions.GetDataFromDictionary("code", ServiceData.RequestFormDictionary);
            string orderNo = PublicFunctions.PublicFunctions.GetDataFromDictionary("orderNo", ServiceData.RequestFormDictionary);
            LogUtils.LogUtil.Info($"接收到的参数：code:{code}");
            AjaxResultModel AjaxResultObject = new AjaxResultModel();
            string strJson = "{\"code\": \"" + code + "\",\"orderNo\": \"" + orderNo + "\"}";
            //string strJson = "{\"codes\": [\"" + code + "\"]}";
            string signature = RSAFromPkcs.sign(strJson, PrivateKey); //签名
            Header header = new Header();
            header.protocol = "POST"; //请求方式
            header.source = OAAppID; //调用系统的appid
            header.target = HisAppID; //被调用系统的appid
            header.type = "signature";//签名方式调用
            header.path = "/openapi/examinePrescription/findExamineResult";//调用的方法
            string headerJson = JsonConvert.SerializeObject(header);
            string headerBase64 = Base64Encrypt.EncodeBase64(Encoding.UTF8, headerJson); //Base64编码
            NameValueCollection headerCollection = new NameValueCollection();
            headerCollection.Add("appid", HisAppID);
            headerCollection.Add("signature", signature);
            headerCollection.Add("header", headerBase64);
            var result = HttpUtil.Post(InterfaceUrl, strJson, "application/json;charset=UTF-8", headerCollection);
            LogUtils.LogUtil.Info($"调用业务系统检查单检查项列表接口结果集：{JsonConvert.SerializeObject(result)}");
            if (result.StatusCode == 200)
            {
                AjaxResultObject.IsSuccess = true;
                AjaxResultObject.Result = result.Result;
            }
            else
                AjaxResultObject.Result = null;
            return AjaxResultObject;
        }
        #endregion

        #region ======调用业务统检查单检查项保存接口======
        public AjaxResultModel SaveCheckListItem()
        {
            LogUtils.LogUtil.Info($"======调用业务统检查单检查项保存接口======");
            AjaxResultModel AjaxResultObject = new AjaxResultModel();
            string checklistitemString = PublicFunctions.PublicFunctions.GetDataFromDictionary("checklistitemString", ServiceData.RequestFormDictionary);
            LogUtils.LogUtil.Info($"调用业务统检查单检查项保存接口接收到的参数：{checklistitemString}");
            string signature = RSAFromPkcs.sign(checklistitemString, PrivateKey); //签名
            Header header = new Header();
            header.protocol = "POST"; //请求方式
            header.source = OAAppID; //调用系统的appid
            header.target = HisAppID; //被调用系统的appid
            header.type = "signature";//签名方式调用
            header.path = "/openapi/examinePrescription/save";//调用的方法
            string headerJson = JsonConvert.SerializeObject(header);
            string headerBase64 = Base64Encrypt.EncodeBase64(Encoding.UTF8, headerJson); //Base64编码
            NameValueCollection headerCollection = new NameValueCollection();
            headerCollection.Add("appid", HisAppID);
            headerCollection.Add("signature", signature);
            headerCollection.Add("header", headerBase64);
            var result = HttpUtil.Post(InterfaceUrl, checklistitemString, "application/json;charset=UTF-8", headerCollection);
            LogUtils.LogUtil.Info($"调用业务统检查单检查项保存接口结果集：{JsonConvert.SerializeObject(result)}");
            if (result.StatusCode == 200)
            {
                AjaxResultObject.IsSuccess = true;
                AjaxResultObject.Result = result.Result;
            }
            else
                AjaxResultObject.Result = null;
            return AjaxResultObject;
        }
        #endregion
    }
}
